﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BtoBSWT315.User
{
    public partial class JobManage : System.Web.UI.Page
    {
        public string sql1;
        int StartIndex = 0;


        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (Session["user"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            if (!IsPostBack)
            {
                DataGridDataBind();
            }
        }

        private void DataGridDataBind()
        {
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();

            string sql = string.Empty;

            sql = "select * from job  inner join qyml on job.gsid=qyml.id where qyml.user='" + Session["user"] + "'" + " order by job.id desc";

            //TODO 二期工作，当搜索出结果只有一条的时候，而StartIndex=2，或者3的时候会出现CRASH
            //switch (DropDownList1.SelectedValue.ToString())
            //{
            //    case "0":
            //        sql = "select * from job  inner join qyml on job.gsid=qyml.id where qyml.user='" + Session["user"] + "'" + " order by job.id desc";
            //        break;
            //    case "招聘职位":
            //        sql = "select * from job inner join qyml on job.gsid=qyml.id  where showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by job.id desc";
            //        break;
            //    case "招聘类型":
            //        sql = "select * from job  inner join qyml on job.gsid=qyml.id where type like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by job.id desc";
            //        break;
            //}

            //创建数据集
            DataSet ds = new DataSet();

            //从指定的索引开始取PageSize条记录
            classdb.Getda(sql).Fill(ds, StartIndex, DataGrid1.PageSize, "CurDataTable");

            //填充所有数集
            classdb.Getda(sql).Fill(ds, "AllDataTable");

            //设置DataGrid控件实际显示的项数
            DataGrid1.VirtualItemCount = ds.Tables["AllDataTable"].Rows.Count;

            //绑定数据源
            DataGrid1.DataSource = ds.Tables["CurDataTable"];
            DataGrid1.DataBind();
        }

        private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            //设置DataGrid当前页的索引值为用户选择的页的索引
            DataGrid1.CurrentPageIndex = e.NewPageIndex;

            //取得当前页为止总共有多少条记录，以便在下一页就从该记录开始读取
            StartIndex = DataGrid1.PageSize * DataGrid1.CurrentPageIndex;

            //重新绑定数据
            DataGridDataBind();
        }

        private void changeRowColor(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            //如果是数据项并且是交替项
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //添加自定义属性，当鼠标移过来时设置该行的背景色为"E8F4FF",并保存原背景色
                e.Item.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='E8F4FF'");
                //添加自定义属性，当鼠标移走时还原该行的背景色
                e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");

            }

        }

        private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            //判断如果是DataGrid控件中的项、交替项以及编辑项
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
            {
                TableCell myTableCell;
                //定义对“删除”单元格的引用

                myTableCell = e.Item.Cells[4];
                //定义对“删除”单元格中控件的引用
                LinkButton btnDel = (LinkButton)myTableCell.Controls[0];
                //添加当单击时弹出确认对话框的自定义属性
                btnDel.Attributes.Add("onclick", "return confirm('您真的要删除此行吗？');");
                btnDel.Text = "<img src=images/del.gif border=0>";
            }
        }

        private void DataGrid1_DeleteCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if (Session["user"] != null)
            {
                Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
                int empID = (int)DataGrid1.DataKeys[e.Item.ItemIndex];
                string sql = "delete DISTINCTROW job.* from job inner JOIN  qyml on job.gsid=qyml.id where job.ID=" + empID + ""+ "and qyml.user='"+Session["user"].ToString()+"'";

                //引入数据库操作类
                classdb.GetNonResult(sql);
                //重新绑定数据
                DataGridDataBind();
            }
            else
            {
                Response.Redirect("../Default.aspx");
            }
        }


        private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (Session["user"] != null)
            {
                switch (DropDownList1.SelectedValue.ToString())
                {
                    case "0":
                        sql1 = "select * from job  inner join qyml on job.gsid=qyml.id where qyml.user='" + Session["user"] + "'" + " order by job.id desc";
                        break;
                    case "招聘职位":
                        sql1 = "select * from job inner join qyml on job.gsid=qyml.id  where showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by job.id desc";
                        break;
                    case "招聘类型":
                        sql1 = "select * from job  inner join qyml on job.gsid=qyml.id where type like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by job.id desc";
                        break;
                }

                //绑定数据源 
                Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
                DataGrid1.DataSource = classdb.GetTables(sql1).DefaultView;
                DataGrid1.DataBind();
            }
            else
            {
                Response.Redirect("../Default.aspx");
            }

        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
            this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
            this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
            this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
            this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.changeRowColor);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}